package cn.learn.service.cargo.impl;

import cn.learn.dao.cargo.InvoiceDao;
import cn.learn.dao.cargo.ShippingOrderDao;
import cn.learn.domain.cargo.Invoice;
import cn.learn.domain.cargo.InvoiceExample;
import cn.learn.domain.cargo.ShippingOrder;
import cn.learn.service.cargo.InvoiceService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class InvoiceServiceImpl implements InvoiceService {

    @Autowired
    private InvoiceDao invoiceDao;
    @Autowired
    private ShippingOrderDao shippingOrderDao;

    @Override
    public PageInfo findAll(InvoiceExample invoiceExample, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Invoice> list = invoiceDao.selectByExample(invoiceExample);
        return new PageInfo(list);
    }

    @Override
    public void save(Invoice invoice) {
        invoice.setCreateTime(new Date());
        invoice.setState(0);
        ShippingOrder shippingOrder = shippingOrderDao.selectByPrimaryKey(invoice.getInvoiceId());
        shippingOrder.setState(2);
        shippingOrderDao.updateByPrimaryKeySelective(shippingOrder);
        invoiceDao.insertSelective(invoice);
    }

    @Override
    public Invoice findById(String id) {
        Invoice invoice = invoiceDao.selectByPrimaryKey(id);
        return invoice;
    }

    @Override
    public void update(Invoice invoice) {
        invoiceDao.updateByPrimaryKeySelective(invoice);
    }

    @Override
    public void delete(String id) {
        invoiceDao.deleteByPrimaryKey(id);
    }
}
